//
//  QZMyVideoVC.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/6/20.
//

#import "QZMyVideoVC.h"
#import "XRWaterfallLayout.h"
#import "QZMyVideoCell.h"
#import "SYCollectionViewRoundFlowLayout.h"
#import "QZMyDetailsPlayVC.h"

@interface QZMyVideoVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, copy) void(^listScrollViewScrollBlock)(UIScrollView *scrollView);

@property (strong, nonatomic)UICollectionView * myCollection;
@property (strong, nonatomic)NSMutableArray <QZVideoModel *>* listArr;
@property (strong, nonatomic)PublicView * nullView;
@property (nonatomic, assign) NSInteger videoPage;
@property (nonatomic, assign) CGSize itemSize;
@end

@implementation QZMyVideoVC
- (NSMutableArray *)sourceData{
    return self.listArr;
}

- (void)updateData:(QZVideoModel *)data index:(NSInteger)index type:(QZDataUpdateType)type{
    if(type == QZDataUPdateDelVideo){
        [self.listArr removeObjectAtIndex:index];
        if(self.listArr.count == 0){
            self.nullView.hidden = NO;
            self.myCollection.clipsToBounds = YES;
        }
        [self.myCollection reloadData];
    }
}
- (void)publicVideo:(QZVideoModel *)data type:(QZDataUpdateType)type{
    self.videoPage = 0;
    [self.myCollection.mj_footer resetNoMoreData];
    [self.listArr removeAllObjects];
    [self myWorksList];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = viewback_black;
    CGFloat w = (KScreen_W-4)/3-0.1;
    _itemSize = CGSizeMake(w, w/3*4);
    KWeakSelf
    self.myCollection.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weakSelf.videoPage = weakSelf.videoPage+1;
        [weakSelf myWorksList];
    }];
    [self myWorksList];
    [[QZDataUpdate share] addListenerObj:self];
}
#pragma mark 懒加载
- (NSMutableArray *)showTextViewToKey1:(NSString *)keyString{
    NSMutableArray *showTextViewArrTo1 = [NSMutableArray new];
    for (int i = 0; [showTextViewArrTo1 count] < 5; i++) {
        int  RandomNumber = arc4random() % 30;
        if(![showTextViewArrTo1 containsObject:[NSNumber numberWithInt:RandomNumber]]){
            [showTextViewArrTo1 addObject:[NSNumber numberWithInt:RandomNumber]];
        }
    }
    return showTextViewArrTo1;
}
- (NSInteger)showTextViewToTime1:(NSString *)time{
    NSDateFormatter *showTextViewFaTo1 = [[NSDateFormatter alloc] init];
    [showTextViewFaTo1 setDateFormat:@"yyyy-MM-dd"];
    return [[showTextViewFaTo1 dateFromString:time] timeIntervalSince1970];
}
- (NSMutableArray *)listArr{
    if(_listArr == nil){
        _listArr = [NSMutableArray new];
    }
    return _listArr;
}
- (UICollectionView *)myCollection{
    if(_myCollection == nil){
        SYCollectionViewRoundFlowLayout *layout = [[SYCollectionViewRoundFlowLayout alloc]init];
        layout.sectionHeadersPinToVisibleBounds = YES;
        layout.collectionCellAlignmentType = SYCollectionViewFlowLayoutAlignmentTypeByLeft;
        _myCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreen_W, KScreen_H-KNav_H-40-KTab_H) collectionViewLayout:layout];
        _myCollection.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _myCollection.backgroundColor = UIColor.clearColor;
        [_myCollection registerClass:[QZMyVideoCell class] forCellWithReuseIdentifier:@"QZMyVideoCell"];
        _myCollection.backgroundColor = viewback_black;
//        //创建瀑布流布局
//        XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:3];
//        //或者一次性设置
//        [waterfall setColumnSpacing:2 rowSpacing:2 sectionInset:UIEdgeInsetsMake(5, 0, KSafeAreaBottomHeight+5, 0)];
        //设置代理，实现代理方法
//        waterfall.delegate = self;
//        _historyCollection.collectionViewLayout = waterfall;
        
        _myCollection.alwaysBounceVertical = YES;
        _myCollection.delegate = self;
        _myCollection.dataSource = self;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_myCollection];
    }
    return _myCollection;
}
- (PublicView *)nullView{
    if(_nullView == nil){
        _nullView = [[PublicView alloc]initWithFrame:CGRectMake(0, (KScreen_H-self.headerHeigh-KTab_H-200)/2, KScreen_W, 200)];
        _nullView.publicLabel.text = @"还没有发布作品哦~";
        _nullView.hidden = YES;
        _nullView.userInteractionEnabled = NO;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_nullView];
    }
    return _nullView;
}
#pragma mark 请求区域
- (void)myWorksList{
    KMutableDic(sendDic)
    sendDic[@"page"] = @(self.videoPage);
    sendDic[@"type"] = @"-1";
    KWeakSelf
    [YBNetBase userVideoListRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            if(weakSelf.videoPage == 0){
                [weakSelf.listArr removeAllObjects];
            }
            for (NSDictionary * dic in data[@"list"]) {
                QZVideoModel * model = [[QZVideoModel alloc]init];
                model.isMyVideo = YES;
                [model setValuesForKeysWithDictionary:dic];
                [weakSelf.listArr addObject:model];
            }
            [weakSelf.myCollection reloadData];
            
            if(weakSelf.listArr.count == 0 && [data[@"list"] count] == 0){
                weakSelf.nullView.hidden = NO;
                weakSelf.myCollection.clipsToBounds = YES;
            }else{
                weakSelf.nullView.hidden = YES;
                weakSelf.myCollection.clipsToBounds = NO;
                if ([data[@"list"] count] == 0) {
                    [weakSelf.myCollection.mj_footer endRefreshingWithNoMoreData];
                }else if([weakSelf.myCollection.mj_footer isRefreshing]){
                    [weakSelf.myCollection.mj_footer endRefreshing];
                }
            }
            
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
#pragma mark - UIScrollViewDelgate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.listScrollViewScrollBlock ? : self.listScrollViewScrollBlock(scrollView);
}
#pragma mark - GKPageListViewDelegate
- (UIScrollView *)listScrollView {
    return self.myCollection;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView * _Nonnull))callback {
    self.listScrollViewScrollBlock = callback;
}

- (UIView *)listView {
    return self.view;
}
#pragma mark collectionDelege
//根据item的宽度与indexPath计算每一个item的高度
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QZMyVideoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QZMyVideoCell" forIndexPath:indexPath];
    cell.videoModel = self.listArr[indexPath.row];
    return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
 layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return _itemSize;
}
- (void)showTextViewSubViewToViewTo1:(UIView * )subView{

    UIView * showTextViewSubViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo1.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo1];
    UILabel * showTextViewSubViewLabelTo2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo2.text = @"56464645";
    [showTextViewSubViewTo1 addSubview:showTextViewSubViewLabelTo2];
    
    UIImageView * showTextViewSubViewTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo2];

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(![self.listArr[indexPath.row].video_url isKindOfClass:[NSDictionary class]] || self.listArr[indexPath.row].video_url == nil || [self.listArr[indexPath.row].video_url allKeys].count == 0){
        [YBMsgPop showPop:@"视频转码中，请稍后在试"];
        return;
    }
    QZMyDetailsPlayVC * play = [[QZMyDetailsPlayVC alloc]init];
    play.videoArr = [[NSMutableArray alloc]initWithArray:self.listArr];
    play.videoPlayIndex = indexPath.row;
    [self.navigationController pushViewController:play animated:YES];
}

@end
